home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Tape Stuff / buffer.c next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  1.3 KB  |  73 lines  |  [TEXT/KAHL]

  1. /* buffer.c */
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "SCSIPrototypes.h"
  6. #include "SCSIDefines.h"
  7. #include "Defines.h"
  8. #include "Prototypes.h"
  9.  
  10.  
  11. BUFFER *BufferOpen(void)
  12. {
  13.     BUFFER *ptr;
  14.     ptr = (BUFFER *)malloc(sizeof(BUFFER));
  15.     if (ptr == NULL) return ptr;
  16.     ptr->index = sizeof(ptr->buffer);
  17.     ptr->write = FALSE;
  18.     return ptr;    
  19. }
  20.  
  21. short BufferRead(BUFFER *ptr, char *data, long count)
  22. {
  23.     long i = 0;
  24.  
  25.     if (ptr == NULL) return ERROR;
  26.     if (ptr->write) return ERROR;
  27.     while (i < count)
  28.     {
  29.         if (ptr->index == sizeof(ptr->buffer))
  30.         {
  31.             if (ReadBlocks(ptr->buffer, BLOCKING) != OK) return ERROR;
  32.             ptr->index = 0;
  33.         }
  34.         *data++ = ptr->buffer[ptr->index++];
  35.         i++;
  36.     }
  37.     return OK;
  38. }
  39.  
  40. short BufferWrite(BUFFER *ptr, char *data, long count)
  41. {
  42.     long i = 0;
  43.     
  44.     if (ptr == NULL) return ERROR;
  45.     ptr->write = TRUE;
  46.     if (ptr->index == sizeof(ptr->buffer)) ptr->index = 0;
  47.     while (i < count)
  48.     {
  49.         ptr->buffer[ptr->index++] = *data++;
  50.         i++;
  51.         if (ptr->index == sizeof(ptr->buffer))
  52.         {
  53.             if (WriteBlocks(ptr->buffer, BLOCKING) != OK) return ERROR;
  54.             ptr->index = 0;
  55.         }
  56.     }
  57.     return OK;
  58. }
  59.  
  60. void BufferClose(BUFFER *ptr)
  61. {
  62.     if (ptr->write)
  63.     {
  64.         if (ptr->index)
  65.         {
  66.             memset(&(ptr->buffer[ptr->index]), 0, sizeof(ptr->buffer) - ptr->index);
  67.             WriteBlocks(ptr->buffer, BLOCKING);
  68.         }
  69.     }
  70.     free(ptr);
  71.     WriteMark(1);
  72. }
  73.